package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sql.ProjectsDao;
import beans.ProjectItemBean;

/**
 * Servlet implementation class GetProjectById
 */
@WebServlet("/GetProjectById")
public class GetProjectById extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetProjectById() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idAsString = request.getParameter("project-id");
		int projectId = 0;
		try {
			if (idAsString != null && ! idAsString.isEmpty()) {
				projectId = Integer.parseInt(idAsString);
			}

			ProjectsDao projectsDao = new ProjectsDao();
			ProjectItemBean projectItemBean = projectsDao.getProjectData(projectId);

			PrintWriter out = response.getWriter();
			out.write(projectItemBean.getJson());
		} catch (ClassNotFoundException | SQLException | NumberFormatException e) {
			e.printStackTrace();
		}
	}

}
